<?php
session_start ();
header ( 'Content-Type: text/html; charset=UTF-8' );
require_once ("../in/tren.php");
$tit = 'Tìm Kiếm Game';
echo '<div class="navigation"><b>Tìm Kiếm Game</b></div><div align="right"><div style="border-top: 3px solid #13cfeb;"></div></div></div><div class="main_menu">';
if (! empty ( $_GET ['key'] )) {
	function danhsach($ar) {
		$file = $ar ['link'];
		$size = filesize ( '../fgame/' . $file . '' ) / 1000;
		echo '<table cellspacing="0" class="block"><tbody><tr><td class="img"><img src="../fgame/' . $ar ['icon'] . '" alt="" height="30" width="30"></td><td><span style="color: #ff0099;">&bull;</span><a href="index.php?id=' . $ar ['id'] . '">' . $ar ['tieude'] . '</a><br/><span style="color: #ff0099;">&bull;</span>Cỡ: ' . $size . 'kb<br/><span style="color: #ff0099;">&bull;</span>' . $ar ['down'] . ' lượt tải.</td></tr></tbody></table><div style="border-top: 1px dotted gray; margin: 1px"></div>';
	}
	$total = mysql_result ( mysql_query ( "SELECT count(*) from game where `tieude` like '%" . mysql_escape_string ( $_GET ['key'] ) . "%';" ), 0 );
	$p = $_GET ['p'];
	if (empty ( $_GET ['p'] ))
		$p = 1;
	if ($p > 1)
		$hal = ' Page #' . $p;
	$show = 10;
	if ($total == "0") {
		echo '<form action="tim.php" method="get"><input type="text" name="key" value="" /><br/><input type="submit" value="Tìm" /></form></div><div class="main_menu"><b>Không tìm thấy gì</b>.</br>Hãy thử tìm tiếp từ khóa khác.</div><div class="main_menu">&laquo;<a href="index.php">Danh Sách Thể Loại</a></div>';
		require_once ("../in/duoi.php");
	} else {
		echo '<form action="tim.php" method="get"><input type="text" name="key" value="" /><br/><input type="submit" value="Tìm" /></form></div><div class="main_menu">Tìm kiếm: "' . htmlspecialchars ( $_GET ['key'] ) . '"</div>';
		$pg = ceil ( $total / $show );
		if ($p > $pg && $p != 1)
			$p = $pg;
		if ($p < 1)
			$p = 1;
		$j = ($p - 1) * $show;
		$a = mysql_query ( "select * from game where `tieude` like '%" . mysql_escape_string ( $_GET ['key'] ) . "%' order by id desc limit $j, $show" );
		while ( $d = mysql_fetch_array ( $a ) ) {
			danhsach ( $d );
		}
		if ($total > $show) {
			echo '<div class="page">';
			$sc = 10;
			$st = floor ( $p / $sc ) * $sc;
			$en = $st + $sc;
			$g = $st;
			if ($g < "2")
				print ('') ;
			else if ($g > "0") {
				if ($g - 1 == 1) {
					$page = '?key=' . $_GET ['key'] . '';
				} else {
					$page = '?key=' . $_GET ['key'] . '&p=' . ($g - 1) . '';
				}
				print ('<a href="' . $page . '">[&laquo;]</a> ') ;
			} else
				print ("") ;
			for($g; ($g < $en); $g ++) {
				if ($g == "1" and $g != $p) {
					print ('<a href="?key=' . $_GET ['key'] . '">1</a> ') ;
				} elseif ($g == "1" and $g == $p) {
					print ('[<b>1</b>] ') ;
				} elseif ($g == $p) {
					print (' [<b>' . $g . '</b>] ') ;
				} elseif ($g <= $pg) {
					if ($g > "0") {
						if ($p != "1") {
							$xx = "$g";
						} else {
							$xx = $g;
						}
						print ('<a href="?key=' . $_GET ['key'] . '&p=' . $xx . '">' . $g . '</a> ') ;
					}
				} else {
					print (' ') ;
				}
			}
			if ($g <= $pg) {
				if (empty ( $_GET ['p'] )) {
					$xx = $g;
				} else {
					$xx = '' . $g;
				}
				print ('<a href="?key=' . $_GET ['key'] . '&p=' . $xx . '">[&raquo;]</a>') ;
			} else
				print (' ') ;
		}
		echo '</div>';
	}
	echo '<div class="main_menu">&laquo;<a href="index.php">Danh Sách Thể Loại</a></div>';
	require_once ("../in/duoi.php");
	exit ();
} else {
	echo '<form action="tim.php" method="get"><input type="text" name="key" value="" /><br/><input type="submit" value="Tìm" /></form></div><div class="main_menu">&laquo;<a href="index.php">Danh Sách Thể Loại</a></div>';
	require_once ("../in/duoi.php");
}
?>

